package com.sdp.right.control;


import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.threadLocal.ContextManager;
import com.sdp.core.bizc.utils.CoreConstantsEnum;
import com.sdp.core.bizc.utils.SnowFlake;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.cache.clean.RightCleanCache;
import com.sdp.right.db.sys_role.service.ISysRoleService;
import com.sdp.right.db.sys_role_menu.service.ISysRoleMenuService;
import com.sdp.right.db.sys_role_person.service.ISysRolePersonService;
import com.sdp.right.entity.SysRole;
import com.sdp.right.utils.RightConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-10-12
 */
@RestController
@RequestMapping("/SysRoleController")
public class SysRoleController {

	@Autowired
	private Query query;
	
	@Autowired
	private SnowFlake snowFlake;

	@Autowired
	private ISysRoleService iSysRoleService;
	
	@Autowired
	private ISysRoleMenuService iSysRoleMenuService;
	
	@Autowired
	private ISysRolePersonService iSysRolePersonService;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private RightCleanCache rightCleanCache;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		//查看当前登录人是否为开发者
		String loginAccountNo = ContextManager.getContextData(CoreConstantsEnum.token中账号的属性名.getCode()).toString();
		List<String> roleNos = iSysRolePersonService.queryRoleNos(loginAccountNo);
		boolean isDeveloper = iSysRoleService.isDeveloper(roleNos);
		
		//如果是开发人员，查看所有角色，否则只能看到业务角色
		if(isDeveloper) {
			return Result.ok(query.page(iSysRoleService, SysRole.class, dataCenter));
		}else {
			QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("role_type", RightConstants.角色类型_业务人员.getCode());
			return Result.ok(query.page(iSysRoleService, SysRole.class, dataCenter,queryWrapper));
		}
	}
	
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody SysRole sysRole) {
		if(sysRole==null) {
			return Result.error("角色信息为空");
		}
		if(StringUtils.isEmpty(sysRole.getKeyId())) {
			
			Result<?> r = uniqueCheckTools.isUniqueSingle(iSysRoleService, SysRole.class, new String[] {"roleName"}, "keyId",sysRole, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("此名称的角色已经存在");
			}
			//默认程序添加的都是业务类型的角色，如果是开发觉得，在数据库修改
			sysRole.setRoleType(RightConstants.角色类型_业务人员.getCode());
			sysRole.setRoleNo(snowFlake.createOrderNo());
			sysRole.setCreateTime(LocalDateTime.now());
			iSysRoleService.save(sysRole);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iSysRoleService, SysRole.class, new String[] {"roleName"}, "keyId",null, sysRole, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("此名称的角色已经存在");
			}
			iSysRoleService.updateById(sysRole);
		}
		//清除缓存
		rightCleanCache.cleanRoleCache();
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="roleNo",required=true) String roleNo) {
		if(StringUtils.isEmpty(roleNo)) {
			return Result.error("角色编号不存在，不能删除");
		}
		iSysRoleService.removeSysRole(roleNo);
		iSysRoleMenuService.removeSysRoleMenuByRoleNo(roleNo);
		iSysRolePersonService.removeSysRolePersonByRoleNo(roleNo);
		return Result.ok();
	}
	
}

